#include "DataAnalysisService.h"

/*!
 * \brief 构造函数
 *
 * \param
 *
 *
 * \return
 *
 * Detail here!!!
 */
DataAnalysisService::DataAnalysisService()
{
    path = QCoreApplication::applicationDirPath();
    conv = new Convertion();
    char * filePath;
    QByteArray tempByte = (path + "/" + CONFIG_FILE_NAME).toLocal8Bit();
    filePath = tempByte.data();
    simplexml = new Simplexml(filePath);
    simplexml->next("analysis-config");
}

/*!
 * \brief 析构函数
 *
 * \param
 *
 *
 * \return
 *
 * Detail here!!!
 */
DataAnalysisService::~DataAnalysisService()
{
    path = "";
    simplexml = nullptr;
    conv = nullptr;
}


/*!
 * \brief 获取衰减开关状态
 *
 * \param
 *
 *
 * \return QString 衰减开关状态(1-开启/0-关闭)
 *
 * Detail here!!!
 */
QString DataAnalysisService::getShuaiJianStatus(){
    QString res = "";
    try {
        res = simplexml->child("shuai-jian-config")->child("shuai-jian-switch")->text();
    } catch (string s) {
        std::cout << s << endl;
        qDebug() << "配置文件信息读取异常" << endl;
    }
    return res;
}

/*!
 * \brief 设置衰减开关状态
 *
 * \param QString 衰减开关状态(1-开启/0-关闭)
 *
 *
 * \return
 *
 * Detail here!!!
 */
void DataAnalysisService::setShuaiJianStatus(QString status){
    try {
        simplexml->child("shuai-jian-config")->child("shuai-jian-switch")->text(status.toStdString().c_str());
        simplexml->save(QCoreApplication::applicationDirPath() + "/" + CONFIG_FILE_NAME); // 保存配置文件
    } catch (string s) {
        std::cout << s << endl;
        qDebug() << "配置文件信息读取异常" << endl;
    }
}

/*!
 * \brief 获取衰减方法
 *
 * \param
 *
 *
 * \return QList<QString> 配置文件中衰减方法
 *
 * Detail here!!!
 */
QList<QString> DataAnalysisService::getShuaiJianMethod(){
    int cnt = 0;
    QList<QString> resList;
    QString res = "";
    try {
        /* 根据衰减方法数量，遍历衰减方法 */
        cnt = atoi(simplexml->child("shuai-jian-config")->child("shuai-jian-num")->text());
        for(int i = 0; i < cnt; i++){
            res = simplexml->child("shuai-jian-config")->child("shuai-jian-method")->child("shuai-jian", i)->text();
            resList.append(res);
        }
    } catch (string s) {
        std::cout << s << endl;
        qDebug() << "配置文件信息读取异常" << endl;
    }
    return resList;
}

/**
  * \brief 获取衰减默认值
  * \param
  * \return int 配置文件中衰减默认值
  */
int DataAnalysisService::getShuaiJianDefaultValue() {
    int defaultValue, num;
    try {
        defaultValue = atoi(simplexml->child("shuai-jian-config")->child("shuai-jian-default-value")->text());
        num = atoi(simplexml->child("shuai-jian-config")->child("shuai-jian-num")->text());
        if(defaultValue >= num)
            defaultValue = -1;
        qDebug() << "DataAnalysisService.cpp line:123" << defaultValue << "num :" << num;
    } catch (string s){
        std::cout << s << endl;
        qDebug() << "配置文件信息读取异常" << endl;
    }
    return defaultValue;
}


/*!
 * \brief 获取滤波开关状态
 *
 * \param
 *
 *
 * \return QString 滤波开关状态(1-开启/0-关闭)
 *
 * Detail here!!!
 */
QString DataAnalysisService::getLvBoStatus(){
    QString res = "";
    try {
        res = simplexml->child("lv-bo-config")->child("lv-bo-switch")->text();
    } catch (string s) {
        std::cout << s << endl;
        qDebug() << "配置文件信息读取异常" << endl;
    }
    return res;
}

/*!
 * \brief 设置滤波开关状态
 *
 * \param QString 滤波开关状态(1-开启/0-关闭)
 *
 *
 * \return
 *
 * Detail here!!!
 */
void DataAnalysisService::setLvBoStatus(QString status){
    try {
        simplexml->child("lv-bo-config")->child("lv-bo-switch")->text(status.toStdString().c_str());
        simplexml->save(QCoreApplication::applicationDirPath() + "/" + CONFIG_FILE_NAME); // 保存配置文件
    } catch (string s) {
        std::cout << s << endl;
        qDebug() << "配置文件信息读取异常" << endl;
    }
}

/*!
 * \brief 获取滤波方法
 *
 * \param
 *
 *
 * \return QList<QString> 配置文件中滤波方法
 *
 * Detail here!!!
 */
QList<QString> DataAnalysisService::getLvBoMethod(){
    int cnt = 0;
    QList<QString> resList;
    QString res = "";
    try {
        /* 根据滤波方法数量，遍历滤波方法 */
        cnt = atoi(simplexml->child("lv-bo-config")->child("lv-bo-num")->text());
        for(int i = 0; i < cnt; i++){
            res = simplexml->child("lv-bo-config")->child("lv-bo-method")->child("lv-bo", i)->text();
            resList.append(res);
        }
    } catch (string s) {
        std::cout << s << endl;
        qDebug() << "配置文件信息读取异常" << endl;
    }
    return resList;
}

/**
 * \brief 获取荧光数据滤波方法默认值
 * \param
 * \return int 获取荧光数据滤波方法默认值
 */
int DataAnalysisService::getLvBoDefaultValue() {
    int defaultValue, num;
    try {
        defaultValue = atoi(simplexml->child("lv-bo-config")->child("lv-bo-default-value")->text());
        num = atoi(simplexml->child("lv-bo-config")->child("lv-bo-num")->text());
        if(defaultValue >= num)
            defaultValue = -1;
        qDebug() << "DataAnalysisService.cpp line:213" << defaultValue << "num :" << num;
    } catch (string s){
        std::cout << s << endl;
        qDebug() << "配置文件信息读取异常" << endl;
    }
    return defaultValue;
}

/*!
 * \brief 获取ct开关状态
 *
 * \param
 *
 *
 * \return QString ct开关状态(1-开启/0-关闭)
 *
 * Detail here!!!
 */
QString DataAnalysisService::getCtStatus(){
    QString res = "";
    try {
        res = simplexml->child("ct-config")->child("ct-switch")->text();
    } catch (string s) {
        std::cout << s << endl;
        qDebug() << "配置文件信息读取异常" << endl;
    }
    return res;
}

/*!
 * \brief 设置滤波开关状态
 *
 * \param QString 滤波开关状态(1-开启/0-关闭)
 *
 *
 * \return
 *
 * Detail here!!!
 */
void DataAnalysisService::setCtStatus(QString status){
    try {
        simplexml->child("ct-config")->child("ct-switch")->text(status.toStdString().c_str());
        simplexml->save(QCoreApplication::applicationDirPath() + "/" + CONFIG_FILE_NAME); // 保存配置文件
    } catch (string s) {
        std::cout << s << endl;
        qDebug() << "配置文件信息读取异常" << endl;
    }
}

/*!
 * \brief 获取Ct值计算方法
 *
 * \param
 *
 *
 * \return QList<QString> Ct值计算方法
 *
 * Detail here!!!
 */
QList<QString> DataAnalysisService::getCtMethod(){
    int cnt = 0;
    QList<QString> resList;
    QString res = "";
    try {
        /* 根据Ct值计算方法数量，遍历Ct值计算方法 */
        cnt = atoi(simplexml->child("ct-config")->child("ct-num")->text());
        for(int i = 0; i < cnt; i++){
            res = simplexml->child("ct-config")->child("ct-method")->child("ct", i)->text();
            resList.append(res);
        }
    } catch (string s) {
        std::cout << s << endl;
        qDebug() << "配置文件信息读取异常" << endl;
    }
    return resList;
}

/**
 * \brief 获取Ct值计算方法默认值
 * \param
 * \return int Ct值计算方法默认值
 */
int DataAnalysisService::getCtDefaultValue() {
    int defaultValue, num;
    try {
        defaultValue = atoi(simplexml->child("ct-config")->child("ct-default-value")->text());
        num = atoi(simplexml->child("ct-config")->child("ct-num")->text());
        if(defaultValue >= num)
            defaultValue = -1;
        qDebug() << "DataAnalysisService.cpp line:302" << defaultValue << "num :" << num;
    } catch (string s){
        std::cout << s << endl;
        qDebug() << "配置文件信息读取异常" << endl;
    }
    return defaultValue;
}

/*!
 * \brief 获取配置文件的默认设置
 *
 * \param
 *
 *
 * \return QString 配置文件的默认设置开关(1-开/0-关)
 *
 * Detail here!!!
 */
QString DataAnalysisService::getDefaultSetting(){
    QString res = "";
    try {
        res = simplexml->child("default-config")->child("default-switch")->text();
    } catch (string s) {
        std::cout << s << endl;
        qDebug() << "配置文件信息读取异常" << endl;
    }
    return res;
}

/*!
 * \brief 设置配置文件的默认设置
 *
 * \param QString 配置文件的默认设置开关(1-开/0-关)
 *
 *
 * \return
 *
 * Detail here!!!
 */
void DataAnalysisService::setDefaultSetting(QString status){
    try {
        simplexml->child("default-config")->child("default-switch")->text(status.toStdString().c_str());
        simplexml->save(QCoreApplication::applicationDirPath() + "/" + CONFIG_FILE_NAME); // 保存配置文件
    } catch (string s) {
        std::cout << s << endl;
        qDebug() << "配置文件信息读取异常" << endl;
    }
}

/*!
 * \brief 获取配置文件中的阈值
 *
 * \param
 *
 *
 * \return QString 获取配置文件中的阈值
 *
 * Detail here!!!
 */
QString DataAnalysisService::getYuZhiSetting(){
    QString res = "";
    try {
        res = simplexml->child("default-config")->child("yu-zhi-num")->text();
    } catch (string s) {
        std::cout << s << endl;
        qDebug() << "配置文件信息读取异常" << endl;
    }
    return res;
}

/*!
 * \brief 获取配置文件中的阈值
 *
 * \param QString 获取配置文件中的阈值
 *
 *
 * \return
 *
 * Detail here!!!
 */
void DataAnalysisService::setYuZhiSetting(QString yuZhi){
    try {
        simplexml->child("default-config")->child("yu-zhi-num")->text(yuZhi.toStdString().c_str());
        simplexml->save(QCoreApplication::applicationDirPath() + "/" + CONFIG_FILE_NAME); // 保存配置文件
    } catch (string s) {
        std::cout << s << endl;
        qDebug() << "配置文件信息读取异常" << endl;
    }
}

/*!
 * \brief 获取配置文件中的基线组
 *
 * \param
 *
 *
 * \return QString 配置文件中的基线组
 *
 * Detail here!!!
 */
QList<int> DataAnalysisService::getJiXianSetting(){
    QList<int> jiXianList; // 结果基线组
    QString startJiXian = "", // 起始基线
            endJiXian = ""; // 结束基线
    try {
        startJiXian = simplexml->child("default-config")->child("ji-xian-begin")->text();
        endJiXian = simplexml->child("default-config")->child("ji-xian-end")->text();
        jiXianList.append(startJiXian.toInt());
        jiXianList.append(endJiXian.toInt());
    } catch (string s) {
        std::cout << s << endl;
        qDebug() << "配置文件信息读取异常" << endl;
    }
    return jiXianList;
}

/*!
 * \brief 设置配置文件的开始基线
 *
 * \param int 开始基线
 *
 *
 * \return
 *
 * Detail here!!!
 */
void DataAnalysisService::setStartJiXianSetting(int start){
    try {
        simplexml->child("default-config")->child("ji-xian-begin")->text(QString::number(start).toStdString().c_str());
        simplexml->save(QCoreApplication::applicationDirPath() + "/" + CONFIG_FILE_NAME); // 保存配置文件
    } catch (string s) {
        std::cout << s << endl;
        qDebug() << "配置文件信息读取异常" << endl;
    }
}

/*!
 * \brief 设置配置文件的结束基线
 *
 * \param int 结束基线
 *
 *
 * \return
 *
 * Detail here!!!
 */
void DataAnalysisService::setEndJiXianSetting(int end){
    try {
        simplexml->child("default-config")->child("ji-xian-end")->text(QString::number(end).toStdString().c_str());
        simplexml->save(QCoreApplication::applicationDirPath() + "/" + CONFIG_FILE_NAME); // 保存配置文件
    } catch (string s) {
        std::cout << s << endl;
        qDebug() << "配置文件信息读取异常" << endl;
    }
}
